<?php

class TaxcodeController extends Controller
{

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions

                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

	public function actionTestcustomer()
	{
		echo "ramdani";
	}

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id, $isnew = NULL)
    {
        $model = NULL;
        if ($isnew == 1)
            $model = new Taxcode();
        else
            $model = $this->loadModel($id);
        if ($model == NULL) {
            throw new CHttpException(404, 'Your requested taxcode can not find');
        }


        if (isset($_POST)) {
            $model->attributes = $_POST;
            if (isset($_POST['account'])) {
                $account = Coa::model()->find('account=:account', array('account' => $_POST['account']));
                if (isset($account))
                    $model->accountdesc = $account->desc1;
            }
            if ($model->save())
                echo 'data saved';
            else
                echo "Create failed: " . json_encode($model->getErrors());
        }
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionGetTaxList($page = 1, $sort = NULL, $dir = NULL, $query = NULL
    )
    {
        //we decrease $page by 1 because in Extjs first page is 1, but in our sql first page is 0;
        if ($page > 0)
            $page--;
        $order = CJSON::decode($sort);
        $criteria = new CDbCriteria();
        if (!empty($query)) {
            $criteria->compare('id', $query, true, 'or');
            $criteria->compare('zipcode', $query, 'or');
            $criteria->compare('salestaxrate', $query, true, 'or');
            $criteria->compare('ratestate', $query, true, 'or');
            $criteria->compare('reportingcodestate', $query, true, 'or');
            $criteria->compare('ratecounty', $query, true, 'or');
            $criteria->compare('reportingcodecounty', $query, true, 'or');
            $criteria->compare('ratecity', $query, true, 'or');
            $criteria->compare('reportingcodecity', $query, true, 'or');
            $criteria->compare('ratespecialdistrict', $query, true, 'or');
            $criteria->compare('reportingcodespecialdistrict', $query, true, 'or');
            $criteria->compare('city', $query, true, 'or');
            $criteria->compare('postoffice', $query, true, 'or');
            $criteria->compare('state', $query, true, 'or');
            $criteria->compare('county', $query, true, 'or');
            $criteria->compare('vendor', $query, true, 'or');
            $criteria->compare('account', $query, true, 'or');
            $criteria->compare('accountdesc', $query, true, 'or');
        }
        $criteria->order = isset($order) ? $order[0]['property'] . ' ' . $order[0]['direction'] : NULL;
        $taxcodes = new CActiveDataProvider('Taxcode', array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 20,
                'currentPage' => $page,
            ),
        ));

        $taxList = array();
        if ($taxcodes->getTotalItemCount() > 0) {
            $taxList['totalCount'] = $taxcodes->getTotalItemCount();
            $taxList['type'] = 'EmpGroup';
            foreach ($taxcodes->getData() as $item) {
                $taxList['data'][] = $item->getAttributes();
            }
            echo json_encode($taxList);
        }
        Yii::app()->end();
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        outputJsByList(Coa::model()->getListWithDesc(), 'coalist');
        outputJsByList(Vendors::model()->getList(), 'vendorslist');
        $this->render('index', array(
        ));
    }


    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model = Taxcode::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'taxcodes-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionTaxData($state,$zipcode){
        $model = Taxcode::model()->find('state=:state and zipcode=:zipcode',array(
            'state'=>$state,
            'zipcode'=>$zipcode
        ));
        if($model!=null){
            $html = $this->renderPartial('taxData',array(
                'model'=>$model
            ),true,false);

           echo $html;
        }
        //$this->showAjaxMessage('Taxcode can not be found',false);
    }

}
